<?php
// +----------------------------------------------------------------------
// | Author: heizai <876555425@qq.com>
// +----------------------------------------------------------------------
namespace plugins\hz_msg_borad\controller;

use cmf\controller\PluginAdminBaseController;
use plugins\hz_msg_borad\model\PluginMessageModel;
use think\Db;

class AdminIndexController extends PluginAdminBaseController
{

    protected function _initialize()
    {
        parent::_initialize();
        $adminId = cmf_get_current_admin_id();//获取后台管理员id，可判断是否登录
        if (!empty($adminId)) {
            $this->assign("admin_id", $adminId);
        }
    }

    /**
     * Message list
     * @adminMenu(
     *     'name'   => 'Message list',
     *     'parent' => 'admin/Plugin/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => 'Demo plug-in',
     *     'param'  => ''
     * )
     */
    public function index()
    {

        $param      = $this->request->param();
        $type       = isset($param['type']) ? $param['type'] : '' ;
        $start_time = isset($param['start_time']) ? $param['start_time'] : '' ;
        $end_time   = isset($param['end_time']) ? $param['end_time'] : '' ;
        $where = [];
        if ($type) {
            $where['type'] = ['eq',$type];
        }

        if ($start_time && empty($end_time)) {
            $where['createtime'] = ['>=',$start_time];
        }else if (empty($start_time) && $end_time) {
            $where['createtime'] = ['<=',$end_time];
        }else if($start_time && $end_time){
            $where['createtime'] = ['>=',$start_time];
            $where['createtime'] = ['<=',$end_time];
        }
        // print_r($where);die;
        $model = new PluginMessageModel();
        $datas = $model->where($where)->order('id desc')->paginate(10)->each(function($item,$k){
            $item['cat_name'] = $item['cat_id'] > 0 ? Db::name('guestbook_cat')->where(array('id'=>$item['cat_id']))->value('cat_name') : '' ;
            $item['date']     = $item['date'] > 0 ? $item['date'] : '' ;
            return $item;
        });
        $this->assign("datas", $datas);
        $this->assign("type", $type);
        $this->assign("start_time", $start_time);
        $this->assign("end_time", $end_time);
        $this->assign('page', $datas->render());
        return $this->fetch('/admin_index');
    }
}
